﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.Models;
using WoSoft.PACS.Data.DataSourceData;
using DevComponents.DotNetBar;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmDataSourceConfig : FrmBase
    {
        private int _id = 0;
        DataSourceService dss = new DataSourceService();

        public FrmDataSourceConfig(int id)
        {
            InitializeComponent();
            _id = id;
        }

        /// <summary>
        /// 确定保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            var model = CheckData();
            if (model != null)
            {
                var cnt = 0;
                if (_id == 0)
                {
                    cnt = dss.InsertData(model);
                }
                else
                {
                    cnt = dss.UpdateData(model);
                }

                if (cnt > 0)
                {
                    MessageBoxEx.Show("执行操作成功");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
        }

        public DataSourceModel CheckData()
        {

            this.errorProvider1.Clear();

            if (((DevComponents.Editors.ComboItem)this.cbYwType.SelectedItem).Value.ToString() == string.Empty)
            {
                this.errorProvider1.SetError(this.cbYwType, "请选择业务类型");
                return null;
            }

            if (this.tbName.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbName, "请输入数据源名称");
                return null;
            }

            //没有选择数据库类型
            if (cbDBType.SelectedItem == null)
            {
                this.errorProvider1.SetError(this.cbDBType, "请选择数据库类型");
                return null;
            }

            //服务器的IP地址
            if (tbIpaddress.Value == null)
            {
                this.errorProvider1.SetError(this.tbIpaddress, "请输入数据库IP地址");
                return null;
            }

            //检查登录用户名
            if (this.tbUserName.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbUserName, "请输入登录用户名");
                return null;
            }

            //登录密码
            if (this.tbPwd.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbPwd, "请输入登录密码");
                return null;
            }

            //数据获取
            var model = new DataSourceModel()
            {
                ID = _id,
                YwType =((DevComponents.Editors.ComboItem)this.cbYwType.SelectedItem).Value.ToString(),
                Name = this.tbName.Text.Trim(),
                DatabaseName = this.tbDatabaseName.Text.Trim(),
                DatabaseType = this.cbDBType.SelectedIndex == 0 ? 1 : 2,
                Instance = this.tbInstanceName.Text.Trim(),
                ServerIP = this.tbIpaddress.Value.Trim(),
                UserName = this.tbUserName.Text.Trim(),
                Password = this.tbPwd.Text.Trim()
            };

            return model;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //加载数据
        private void FrmDataSourceConfig_Load(object sender, EventArgs e)
        {
            if (_id > 0)
            {
                var model = dss.GetModelByID(_id);
                if (model != null)
                {
                    this.cbYwType.Text = model.YwType;
                    this.tbName.Text = model.Name;
                    this.cbDBType.SelectedIndex = model.DatabaseType == 1 ? 0 : 1;
                    this.tbInstanceName.Text = model.Instance;
                    this.tbIpaddress.Value = model.ServerIP;
                    this.tbUserName.Text = model.UserName;
                    this.tbPwd.Text = model.Password;
                    this.tbDatabaseName.Text = model.DatabaseName;
                }
            }
        }
    }
}
